function sendCodeJudge() {
    var phone = document.getElementById("phoneText").value;
    if (phone.length !== 11) {
        alert("请先输入正确的手机号！");
    }
    else {
        $.ajax({
            url:'sendCodeServlet',
            type: 'POST',
            dataType: "json",
            data:{
                phoneText:phone
            },
            success:function (data) {
                if (data == false){
                    window.alert("该用户不存在！");
                }
                else {
                    window.alert("验证短信已发送！")
                }
            }
        })
    }
}


function login() {
    var phone = document.getElementById("phoneText").value;
    var code = document.getElementById("code").value;
    $.ajax({
        url:'loginWebServlet',
        type: 'POST',
        dataType: "json",
        data:{
            phoneText:phone,
            code : code
        },
        success:function (data) {
            if (data == false)
            window.alert("验证码错误！");
            else {
                document.getElementById("loginForm").action = "index.jsp";
                document.getElementById("loginForm").submit();
            }
        }
    })
}

function send() {
    var phone = document.getElementById("phoneText").value;
    if (phone.length !== 11) {
        alert("请先输入正确的手机号！");
    }
    else {
        currentCount = 60;
        document.getElementById("sendCodeBtn").setAttribute("disabled", "true");
        $.ajax({
            type: 'POST',
            dataType: "json",
            url:'sendCodeServlet',
            data:{
                phoneText:phone
            },
            success:function (data) {
                if (data == false){
                    window.alert("该用户不存在！");
                    document.getElementById("sendCodeBtn").removeAttribute("disabled");
                    document.getElementById("sendCodeBtn").value = "重获验证码";
                }
                else {
                    window.alert("验证短信已发送！");
                    document.getElementById("sendCodeBtn").value = currentCount + "秒后重获";
                    time = window.setInterval("setRemainTime()", 1000);
                }
            }
        });
    }
}

function setRemainTime() {
    if (currentCount == 0) {
        window.clearInterval(time); //停止计时器
        document.getElementById("sendCodeBtn").removeAttribute("disabled");
        document.getElementById("sendCodeBtn").value = "重获验证码";
    }
    else {
        currentCount--;
        document.getElementById("sendCodeBtn").value = currentCount + "秒后重获";
    }
}

var time;
var count = 60;
var currentCount;
